package com.beidasoft.web.config.dataSourceConfig;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * Created by hoax on 2021-02-22 17:59.
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
    static final String PACKAGE = "com.beidasoft.web.dao.master";
    static final String MAPPER_LOCATION = "classpath:com/beidasoft/web/dao/master/*.xml";

    @Value("${masterMysql.datasource.url}")
    private String url;

    @Value("${masterMysql.datasource.username}")
    private String user;

    @Value("${masterMysql.datasource.password}")
    private String password;

    @Value("${masterMysql.datasource.driver-class-name}")
    private String driverClass;

//    @Value("${masterMysql.datasource.min-idle}")
//    private int minIdle;
//
//    @Value("${masterMysql.datasource.max-active}")
//    private int maxActive;
//
//    @Value("${masterMysql.datasource.max-wait}")
//    private int maxWait;
//
//    @Value("${masterMysql.datasource.min-evictable-idle-time-millis}")
//    private int idleWait;
//
//
//    @Value("${masterMysql.datasource.maximum-pool-size}")
//    private int maxPoolSize;


    @Bean(name = "masterDataSource")
    @Primary
    public DataSource masterDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
//        dataSource.setMaxLifetime(0);
//        dataSource.setIdleTimeout(0);
//
//        dataSource.setMinimumIdle(minIdle);
//        dataSource.setMaximumPoolSize(maxPoolSize);
//
//        dataSource.setConnectionTimeout(maxWait);
        //dataSource.setIdleTimeout(idleWait);
        return dataSource;
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.masterDataSource());
        return transactionManager;
    }

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
        sessionFactory.setTypeAliasesPackage("com.beidasoft.web.model,com.beidasoft.web.vo");
        return sessionFactory.getObject();
    }
}